﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Arena_Gangrena
{
    class c_Animacao
    {
        Texture2D spriteStrip;
        float escala;
        int elapsedTime;
        int frameTime;
        int frameCount;
        int frame_atual;
        Color color;
        Rectangle sourceRect;
        Rectangle destinationRect;

        public int FrameWidth;
        public int FrameHeight;
        public bool Active;
        public bool Looping;
        public Vector2 Position;

        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frameTime, Color color, float scale, bool looping)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frameTime;
            this.escala = scale;

            Looping = looping;
            Position = position;
            spriteStrip = texture;

            elapsedTime = 0;
            frame_atual = 0;

            Active = true;
        }

        public void Update(GameTime gameTime)
        {
            if (Active == false)
                return;

            elapsedTime += (int)gameTime.TotalGameTime.TotalSeconds;

            if (elapsedTime > frameTime)
            {
                frame_atual++;

                if (frame_atual == frameCount)
                {
                    frame_atual = 0;

                    if (Looping == false)
                        Active = false;
                }

                elapsedTime = 0;
            }
            sourceRect = new Rectangle(frame_atual * FrameWidth, 0, FrameWidth, FrameHeight);
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * escala) / 2,
                                            (int)Position.Y - (int)(FrameHeight * escala) / 2,
                                            (int)(FrameWidth * escala),
                                            (int)(FrameHeight * escala));
        }

        public void draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
            }
        }
    }
}
